/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2023. All rights reserved.
 */

package dao

import (
	"fmt"
	"prim-server/common"
	"prim-server/model"

	"github.com/astaxie/beego/orm"
	"github.com/pkg/errors"
	"github.com/spf13/cast"
)

type AssetDao struct {
	BaseDao
}

type IAssetDao interface {
	SaveAssetInfo(asset *model.Asset) error
	DeleteAssetInfo(assertId string) error
	UpdateAssetInfo(asset *model.Asset) error
	GetAssetInfo(queryParameter *common.QueryAssetDetail) (asset []*model.Asset, totalCount int, err error)
}

func GetAssetDao() IAssetDao {
	dao := &AssetDao{}
	dao.Init()
	return dao
}

func (ad *AssetDao) SaveAssetInfo(asset *model.Asset) error {
	if asset == nil {
		return nil
	}
	_, err := ad.O.Insert(asset)
	if err != nil {
		return errors.Errorf("SaveAssetInfo error: %v, assetId: %s", err, asset.AssetId)
	}
	return nil
}

func (ad *AssetDao) DeleteAssetInfo(assertId string) error {
	if len(assertId) == 0 {
		return nil
	}
	asset := new(model.Asset)
	asset.AssetId = assertId
	_, err := ad.O.Delete(asset, "asset_id")
	if err != nil {
		return errors.Errorf("DeleteAssetInfo error: %v, assetId: %s", err, asset.AssetId)
	}
	return nil
}
func (ad *AssetDao) UpdateAssetInfo(asset *model.Asset) error {
	if asset == nil {
		return nil
	}
	_, err := ad.O.Update(asset)
	if err != nil {
		return errors.Errorf("UpdateAssetInfo error: %v, assetId: %s", err, asset.AssetId)
	}
	return nil
}
func (ad *AssetDao) GetAssetInfo(queryParameter *common.QueryAssetDetail) (asset []*model.Asset, totalCount int, err error) {
	if queryParameter == nil {
		return nil, 0, nil
	}
	recode := ad.O.QueryTable(&model.Asset{})
	finalCondition := orm.NewCondition()
	if len(queryParameter.AssetId) > 0 {
		finalCondition = finalCondition.And("asset_id__in", queryParameter.AssetId)
	}
	if len(queryParameter.UserId) > 0 {
		finalCondition = finalCondition.And("user_id__in", queryParameter.UserId)
	}
	if len(queryParameter.ProductionType) > 0 {
		finalCondition = finalCondition.And("production_type__in", queryParameter.ProductionType)
	}
	if len(queryParameter.ResourceType) > 0 {
		finalCondition = finalCondition.And("resource_type__in", queryParameter.ResourceType)
	}
	if len(queryParameter.ResourceStatus) > 0 {
		finalCondition = finalCondition.And("resource_status__in", queryParameter.ResourceStatus)
	}
	if len(queryParameter.CloudResourceId) > 0 {
		finalCondition = finalCondition.And("cloud_resource_id__in", queryParameter.CloudResourceId)
	}
	if len(queryParameter.AssociatedOrderId) > 0 {
		finalCondition = finalCondition.And("associated_order_id__in", queryParameter.AssociatedOrderId)
	}
	if len(queryParameter.AssociatedCloudOrderId) > 0 {
		finalCondition = finalCondition.And("associated_cloud_order_id__in", queryParameter.AssociatedCloudOrderId)
	}
	offset, pageSize := ad.GetOffsetAndPageSize(queryParameter.Page, queryParameter.PageSize)
	recode = recode.SetCond(finalCondition)
	totalCount64, err := recode.Count()
	if err != nil {
		return nil, 0, errors.Errorf("GetAssetInfo error: %v, userId: %s", err, fmt.Sprint(queryParameter.UserId))
	}
	totalCount = cast.ToInt(totalCount64)
	_, err = recode.Offset(offset).Limit(pageSize).All(&asset)
	if err != nil {
		if err == orm.ErrNoRows {
			//not found
			return nil, 0, nil
		}
		//found error
		return nil, 0, errors.Errorf("GetAssetInfo error: %v, userId: %s", err, fmt.Sprint(queryParameter.UserId))
	}
	return asset, totalCount, nil
}
